
var Genders = [{ id:'表单', text: '表单' }, { id: '报表', text: '报表'}];

mini.parse();

var grid = mini.get("datagrid1");

var tmrow=JSON.parse(localStorage.getItem("row"));
var modelId = tmrow.id;
grid.set({url:ajaxUrl + "/PrintList/findByModelId.do?modelId="+modelId});
grid.reload();
function reloaddata() {
    //grid.url=ajaxUrl + "/printTemplate/findByVouchId.do?cVouchID="+tmrow.id;
    grid.set({url:ajaxUrl + "/printTemplate/findByVouchId.do?cVouchID="+modelId});
    grid.reload();
}

function onKeyEnter(e) {
    reloaddata();
}

function addRow() {
    var newRow = { modelId:""+modelId+""};
    grid.addRow(newRow, 0);

    grid.beginEditCell(newRow, "cTyep");
}
function  update() {
    grid.allowCellEdit=true;
    mini.get("but_update").disable();
    mini.get("but_save").enable();
    mini.get("but_remove").enable();
    mini.get("but_add").enable();
    mini.get("but_reload").disable();
    mini.get("but_getup").enable();
}
function getup() {
    grid.allowCellEdit=false;
    mini.get("but_update").enable();
    mini.get("but_save").disable();
    mini.get("but_remove").disable();
    mini.get("but_add").disable();
    mini.get("but_reload").enable();
    mini.get("but_getup").disable();
}
function onSearch() {
    var key = mini.get("key").getValue();
    if(key!=""){
        grid.load({tablename:"PrintTemplate",cWhereStr:" and cTemplateName like ''%"+key+"%''"});
    }else{
        grid.reload();
    }
}
// 删除模板
function removeRow() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        var printIds = [];
        // 得到模板id
        rows.forEach(function (item) {
            printIds.push(item.printId);
        });
        // 参数
        printIds = printIds.join(',');
        // 请求中等待遮罩
        grid.loading('请稍后...')
        $.ajax({
            url: ajaxUrl + '/PrintList/deletePrintList.do?ids=' + printIds,
            type: 'POST',
            success: function (data) {
                data = JSON.parse(data);
                // loading遮罩取消
                grid.unmask();
                // 提示
                mini.alert(data.state);
                // 刷新模板表格
                grid.reload();
            },
            error: function (data) {
                mini.alert('请求失败');
            }
        })
        //grid.removeRows(rows, true);
    }else{
        mini.alert('请选择需要删除的模板');
    }
}
function saveData() {
    grid.commitEdit();
    var rowData = grid.getChanges();
    // 删除不需要的数据
    rowData.forEach(function (item){
        delete item._state;
        delete item._id;
        delete item._uid;
    });
    grid.loading("保存中，请稍后......");
    var json = mini.encode({"entityName": "PrintingList","data":rowData});
    $.ajax({
        type:"post",
        url: ajaxUrl + "/PrintList/insert.do",
        data: json,
        contentType: 'application/json',
        success: function (text) {
            grid.reload();
            getup();
        },
        error: function (jqXHR, textStatus, errorThrown) {
            mini.alert(jqXHR.responseText);
        }
    });
}


grid.on("celleditenter", function (e) {
    var index = grid.indexOf(e.record);
    if (index == grid.getData().length - 1) {
        var row = {};
        grid.addRow(row);
    }
});

grid.on("beforeload", function (e) {
    if (grid.getChanges().length > 0) {
        if (confirm("有增删改的数据未保存，是否取消本次操作？")) {
            e.cancel = true;
        }
    }
});
function onMarriedRenderer(e) {
    if (e.value == 1) return "是";
    else return "否";
}
function onActionRenderer2(e) {
    var rowIndex = e.rowIndex;
    var row = grid.getRow(rowIndex);
    var s="";
    if (typeof(row.id)!="undefined" && row.id !=0)
    {
        s = '<a class="New_Button" href="javascript:SeeView(' + rowIndex + ')">打印预览</a>     '

    }

    return s;
}
function onActionRenderer(e) {
    var rowIndex = e.rowIndex;
    var row = grid.getRow(rowIndex);
    var s="";
    if (typeof(row.printId)!="undefined" && row.printId !=0)
    {
        s = '<a class="New_Button" href="javascript:editRow(' + rowIndex + ')">编辑打印模板</a>     '

    }

    return s;
}
function SeeView(rowIndex){
    var row = grid.getRow(rowIndex);
    localStorage.setItem('row',JSON.stringify(row));
    if (row) {
        mini.open({
            url: bootPATH + "../frame/frame1/pages/DesignerPrint/PrintView.html",
            title: "打印预览==>"+row.cTemplateName,
            width: 900,
            height: 1000,
            showMaxButton:true,
            loadOnRefresh:false,
            ondestroy: function (action) {  //弹出页面关闭前
                if (action == "ok") {       //如果点击“确定”
                    var iframe = this.getIFrameEl();
                    row=iframe.contentWindow.thiseditrow;
                    grid.reload();
                }
            }
        });
    }
}
// 编辑打印模板（跳转）
function editRow(rowIndex) {
    var row = grid.getRow(rowIndex);
    // 模板id
    //row.modelId = modelId;
    if(row){
        // 打印数据存储
        localStorage.setItem('row',JSON.stringify(row));
        // 跳转页面参数
        var item = {
            iconCls : "fa fa-desktop",
            id: row.id,
            text : "打印设置："+ row.printingName,

            url : "../pages/printModel/printModel.html"
        };
        parent.activeTab(item);//与父页面通信
    }


    //
    // if (row) {
    //      mini.open({
    //     //     targetWindow:this,
    //
    //          url: bootPATH + "../frame/frame1/pages/DesignerPrint/PrintTest.html",
    //          title: "打印模板编辑==>"+row.cTemplateName,
    //          width: 900,
    //          height: 1000,
    //          showMaxButton:true,
    //          loadOnRefresh:false,
    //          onload: function () {       //弹出页面加载完成
    //              var iframe = this.getIFrameEl();
    //              // iframe.contentWindow.ReportDesigner.Report.LoadFromStr(row.cContent);
    //              //  iframe.contentWindow.thiseditrow=row;
    //              //  iframe.contentWindow.ReportDesigner.Report.LoadFromURL("../../designerprint/getgrf?id="+row.id+"&random="+Math.random());
    //              // iframe.contentWindow.ReportDesigner.Reload();
    //          },
    //         ondestroy: function (action) {  //弹出页面关闭前
    //             if (action == "ok") {       //如果点击“确定”
    //                 var iframe = this.getIFrameEl();
    //                 row=iframe.contentWindow.thiseditrow;
    //                 grid.reload();
    //             }
    //         }
    //     });
    // }
}
